<?php
/**
 * 3-4岁专区认知模块管理页面
 */
require_once '../../../admin/check_session.php';
require_once '../../../includes/config.php';
require_once '../includes/CognitiveManager34.php';

// 创建认知管理器实例
$cognitiveManager = new CognitiveManager34();

// 处理删除操作
if (isset($_POST['delete_module']) && isset($_POST['module_id'])) {
    $moduleId = (int)$_POST['module_id'];
    $result = $cognitiveManager->moduleManager->deleteModule($moduleId);
    
    if ($result) {
        $successMessage = "认知模块已成功删除";
    } else {
        $errorMessage = "删除认知模块时出错";
    }
}

// 处理激活/停用操作
if (isset($_POST['toggle_status']) && isset($_POST['module_id'])) {
    $moduleId = (int)$_POST['module_id'];
    $module = $cognitiveManager->getModuleById($moduleId);
    
    if ($module) {
        $newStatus = $module['is_active'] ? 0 : 1;
        $result = $cognitiveManager->updateModule($moduleId, ['is_active' => $newStatus]);
        
        if ($result) {
            $successMessage = "认知模块状态已更新";
        } else {
            $errorMessage = "更新认知模块状态时出错";
        }
    }
}

// 获取所有3-4岁认知模块
$modules = $cognitiveManager->getAllModules();

// 添加页面标题
$pageTitle = "3-4岁专区认知模块管理";
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $pageTitle; ?> - KidsMind管理后台</title>
    <!-- 引入管理后台通用样式 -->
    <link rel="stylesheet" href="/admin/css/admin.css">
    <link rel="stylesheet" href="/admin/css/bootstrap.min.css">
    <link rel="stylesheet" href="/admin/css/fontawesome.min.css">
    <!-- 3-4岁专用样式 -->
    <style>
        .module-card {
            border-radius: 15px;
            box-shadow: 0 4px 8px rgba(0,0,0,0.1);
            transition: all 0.3s ease;
            margin-bottom: 20px;
            border: none;
            overflow: hidden;
        }
        .module-card:hover {
            box-shadow: 0 8px 16px rgba(0,0,0,0.15);
            transform: translateY(-5px);
        }
        .module-header {
            padding: 15px;
            border-radius: 15px 15px 0 0;
        }
        .module-body {
            padding: 15px;
            background-color: #fff;
        }
        .module-icon {
            font-size: 2rem;
            margin-right: 10px;
            vertical-align: middle;
        }
        .badge-status {
            font-size: 0.8rem;
            padding: 5px 10px;
            border-radius: 20px;
        }
        .add-module-card {
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 200px;
            background-color: #f8f9fa;
            border: 2px dashed #dee2e6;
            cursor: pointer;
            border-radius: 15px;
            transition: all 0.3s ease;
        }
        .add-module-card:hover {
            background-color: #e9ecef;
            border-color: #adb5bd;
        }
        .add-module-icon {
            font-size: 3rem;
            color: #6c757d;
        }
        /* 马卡龙色系配色 */
        .bg-pastel-pink { background-color: #FFD6E0; }
        .bg-pastel-blue { background-color: #D6E5FF; }
        .bg-pastel-green { background-color: #D6FFE1; }
        .bg-pastel-yellow { background-color: #FFEFD6; }
        .bg-pastel-purple { background-color: #E9D6FF; }
        .border-pastel-pink { border-color: #FFD6E0; }
        .border-pastel-blue { border-color: #D6E5FF; }
        .border-pastel-green { border-color: #D6FFE1; }
        .border-pastel-yellow { border-color: #FFEFD6; }
        .border-pastel-purple { border-color: #E9D6FF; }
    </style>
</head>
<body>
    <?php include '../../../admin/includes/header.php'; ?>
    
    <div class="container-fluid">
        <div class="row">
            <?php include '../../../admin/includes/sidebar.php'; ?>
            
            <main class="col-md-9 ms-sm-auto col-lg-10 px-md-4">
                <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
                    <h1 class="h2">3-4岁专区认知模块管理</h1>
                    <div class="btn-toolbar mb-2 mb-md-0">
                        <div class="btn-group me-2">
                            <a href="cognitive_module_edit.php" class="btn btn-sm btn-outline-primary">添加新模块</a>
                        </div>
                    </div>
                </div>
                
                <?php if (isset($successMessage)): ?>
                <div class="alert alert-success" role="alert">
                    <?php echo $successMessage; ?>
                </div>
                <?php endif; ?>
                
                <?php if (isset($errorMessage)): ?>
                <div class="alert alert-danger" role="alert">
                    <?php echo $errorMessage; ?>
                </div>
                <?php endif; ?>
                
                <div class="row">
                    <?php foreach ($modules as $module): ?>
                    <div class="col-md-4">
                        <div class="card module-card">
                            <div class="module-header bg-<?php echo getModuleColor($module); ?>">
                                <div class="d-flex justify-content-between align-items-center">
                                    <h5 class="mb-0">
                                        <span class="module-icon"><?php echo $module['icon']; ?></span>
                                        <?php echo htmlspecialchars($module['module_name']); ?>
                                    </h5>
                                    <?php if ($module['is_active']): ?>
                                    <span class="badge bg-success badge-status">已激活</span>
                                    <?php else: ?>
                                    <span class="badge bg-secondary badge-status">未激活</span>
                                    <?php endif; ?>
                                </div>
                            </div>
                            <div class="module-body">
                                <p class="text-muted mb-3"><?php echo nl2br(htmlspecialchars($module['description'])); ?></p>
                                
                                <div class="d-flex justify-content-between align-items-center mb-2">
                                    <small class="text-muted">更新于: <?php echo date('Y-m-d H:i', strtotime($module['updated_at'])); ?></small>
                                    <small class="text-muted">排序: <?php echo $module['display_order']; ?></small>
                                </div>
                                
                                <div class="btn-group w-100" role="group">
                                    <a href="cognitive_module_edit.php?id=<?php echo $module['module_id']; ?>" class="btn btn-outline-primary btn-sm">
                                        <i class="fas fa-edit"></i> 编辑
                                    </a>
                                    <a href="cognitive_contents.php?module_id=<?php echo $module['module_id']; ?>" class="btn btn-outline-info btn-sm">
                                        <i class="fas fa-list"></i> 内容
                                    </a>
                                    <form method="post" onsubmit="return confirm('确定要<?php echo $module['is_active'] ? '停用' : '激活'; ?>该模块吗？');" style="display: inline;">
                                        <input type="hidden" name="module_id" value="<?php echo $module['module_id']; ?>">
                                        <button type="submit" name="toggle_status" class="btn btn-outline-<?php echo $module['is_active'] ? 'warning' : 'success'; ?> btn-sm">
                                            <i class="fas fa-<?php echo $module['is_active'] ? 'pause' : 'play'; ?>"></i> 
                                            <?php echo $module['is_active'] ? '停用' : '激活'; ?>
                                        </button>
                                    </form>
                                    <form method="post" onsubmit="return confirm('确定要删除该模块吗？此操作不可撤销！');" style="display: inline;">
                                        <input type="hidden" name="module_id" value="<?php echo $module['module_id']; ?>">
                                        <button type="submit" name="delete_module" class="btn btn-outline-danger btn-sm">
                                            <i class="fas fa-trash"></i> 删除
                                        </button>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
                    <?php endforeach; ?>
                    
                    <!-- 添加新模块卡片 -->
                    <div class="col-md-4">
                        <a href="cognitive_module_edit.php" class="text-decoration-none">
                            <div class="add-module-card">
                                <div class="text-center">
                                    <div class="add-module-icon mb-2">
                                        <i class="fas fa-plus-circle"></i>
                                    </div>
                                    <h5 class="mb-0">添加新模块</h5>
                                </div>
                            </div>
                        </a>
                    </div>
                </div>
            </main>
        </div>
    </div>
    
    <script src="/admin/js/bootstrap.bundle.min.js"></script>
    <script src="/admin/js/jquery-3.6.0.min.js"></script>
    <script>
        // 动态应用马卡龙色系
        function getRandomPastelClass() {
            const pastelClasses = [
                'pastel-pink', 'pastel-blue', 'pastel-green', 
                'pastel-yellow', 'pastel-purple'
            ];
            const randomIndex = Math.floor(Math.random() * pastelClasses.length);
            return pastelClasses[randomIndex];
        }
    </script>
</body>
</html>

<?php
/**
 * 根据模块信息返回对应的色系类
 */
function getModuleColor($module) {
    $colorMap = [
        'red' => 'pastel-pink',
        'blue' => 'pastel-blue',
        'green' => 'pastel-green',
        'yellow' => 'pastel-yellow',
        'purple' => 'pastel-purple'
    ];
    
    if (isset($module['background_color']) && array_key_exists($module['background_color'], $colorMap)) {
        return $colorMap[$module['background_color']];
    }
    
    // 根据模块ID分配固定颜色，确保相同模块总是获得相同颜色
    $colors = array_values($colorMap);
    $index = $module['module_id'] % count($colors);
    return $colors[$index];
} 